package IC.Symbols;

import IC.SemanticError;
import IC.AST.Formal;
import IC.AST.LocalVariable;
import IC.Types.Type;
import IC.Types.TypeTable;

public class MethodSymbolTable extends SymbolTable {
	
	private boolean isStatic;

	public MethodSymbolTable(String id, SymbolTable parent, Type retValue, boolean isStatic) {
		super(id, parent);
		RetVarSymbol symbol = new RetVarSymbol(retValue);
		entries.put("ret", symbol);
		this.isStatic = isStatic;
	}
	
	public boolean isStatic(){
		return isStatic;
	}
	
	public void insert(Formal formal) throws SemanticError{
		String name = formal.getName();
		Type type = TypeTable.getType(formal);
		ParamSymbol symbol = new ParamSymbol(name, type, formal.getOrder());
		Symbol previous = entries.put(name, symbol);
		if (previous != null){
			throw new SemanticError(formal.getLine(), "a parameter with name " + name + " is already defined");
		}
	}
	
	public void insert(LocalVariable var) throws SemanticError{
		String name = var.getName();
		Type type = TypeTable.getType(var);
		VarSymbol symbol = new VarSymbol(name, type, var.getOrder());
		Symbol previous = entries.put(name, symbol);
		
		if (previous != null){
			throw new SemanticError(var.getLine(), "a local variable with name " + name + " is already defined");
		}
	}

	public String getDisplayString(){
		return "Method Symbol Table: " + name;
	}
}
